<?php
namespace fast;


use think\App;

class Meituan
{
    protected static $appKey = 'cb0837db041b7ee2';
    protected static $appSecret = '94e28e14c53a1daac56e0117535e16527476e1be';

    public static function getSession($code)
    {
        $params = [];
        $params['app_key'] = self::$appKey;
        $params['app_secret'] = self::$appSecret;
        $params['auth_code'] = $code;
        $params['grant_type'] = 'authorization_code';
        $params['redirect_url'] = 'http://a.reactphp.club/api/meituan/callback';

        $url = "https://openapi.dianping.com/router/oauth/token";
        $options = [];
        $options[CURLOPT_HTTPHEADER] = [
            'Content-type:application/x-www-form-urlencoded'
        ];
        $result = Http::post($url, $params, $options);
        // echo $result;

        return json_decode($result, true);
    }

    public static function refreshSession()
    {
        $root = App::getInstance()->getRootPath();
        $tokenFile = $root.'/cache/meituan.json';
        $tokenRs = file_get_contents($tokenFile);
        $tokenRs = json_decode($tokenRs,true);
        $params = [];
        $params['app_key'] = self::$appKey;
        $params['app_secret'] = self::$appSecret;
        $params['grant_type'] = 'refresh_token';
        $params['refresh_token'] = $tokenRs['refresh_token'];
        print_r($params);

        $url = "https://openapi.dianping.com/router/oauth/token";
        $options = [];
        $options[CURLOPT_HTTPHEADER] = [
            'Content-type:application/x-www-form-urlencoded'
        ];
        $result = Http::post($url, $params, $options);
        echo $result;
        file_put_contents($tokenFile, $result);

        return json_decode($result, true);
    }

    // 获取差评
    public static function getBadComment($shopUUid, $session='')
    {
        $root = App::getInstance()->getRootPath();
        $tokenRs = file_get_contents($root.'/cache/meituan.json');
        $tokenRs = json_decode($tokenRs,true);
        $now = time();
        $url = "https://openapi.dianping.com/router/ugc/queryshopreview";
        $query = [];
        $query['app_key'] = self::$appKey;
        $query['timestamp'] = date('Y-m-d H:i:s');
        $query['format'] = 'json';
        $query['v'] = 2;
        $query['sign_method'] = 'MD5';
        $query['open_shop_uuid'] = $shopUUid;
        $query['begintime'] = date('Y-m-d H:i:s', $now - 600);
        #$query['begintime'] = '2021-03-01 00:00:00';
        $query['endtime'] = date('Y-m-d H:i:s', $now);
        $query['star'] = 3;
        $query['platform'] = 1;
        $query['offset'] = 1;
        $query['limit'] = 1;

        $query['session'] = $tokenRs['access_token'];

        //计算md5
        $query['sign'] = self::sign($query);
        $params = http_build_query($query);
        $uri = $url.'?'.$params;
        $result = file_get_contents($uri);

        $resultRs = json_decode($result, true);
        if ($resultRs['code'] == 608) {
            self::refreshSession();
        }

        return $resultRs;
    }

    public static function sign(&$query)
    {
        ksort($query);
        $signStr = "";
        foreach ($query as $key=>$value){
            $signStr.=($key.$value);
        }

        return md5(self::$appSecret.$signStr.self::$appSecret);
    }

    public static function getStores()
    {
        $data = [
            '2d61c3a55dcf017fbfe3453d76410710'  => [
                'title' => '花嫁丽舍海岛婚礼殿堂',
                'address' => '新虹桥合川路3088号'
            ],
            '367e559b6bf1efd2c4cd919cd1890627'  => [
                'title' => '花嫁丽舍私人婚礼会所',
                'address' => '浦明路1888号5号楼'
            ],
            'e4f5141d3d986655e42d71f339b3e067'  => [
                'title' => '花嫁丽舍海岛婚礼殿堂',
                'address' => '龙蟠路88号'
            ],
            '439a75c58806bed421505be15455bc23'  => [
                'title' => '花嫁丽舍一站式海岛婚礼殿堂',
                'address' =>'金盏乡蟹岛路1号蟹岛国际会展中心2号馆'
            ],
            '7bd81fce8f1fc1b4ee5411689cd484d7'  => [
                'title' => '花嫁丽舍私人婚礼会所',
                'address' => '江杨南路485号一号楼'
            ],
            'f6f0996e86c20775b2764218555fc56a'  => [
                'title' => '韩国Wedding Only婚纱馆',
                'address' => '国展路489号'
            ],
            '449488f0bc7b2849513cbd7eddc5f409'  => [
                'title' => '花嫁丽舍私人婚礼会所',
                'address' =>'吴中路52号古北生活广场4楼'
            ],
            '9007c301a5855a2bb3fd184b23d934a1'  => [
                'title' => '花嫁丽舍私人婚礼会所',
                'address' => '国权路49号绿色米兰广场王座4楼'
            ],
            '1666b787cde2aa442cec057fedaedd26'  => [
                'title' => '花嫁丽舍婚礼艺术空间',
                'address' => '合川路3088号四号楼'
            ],
            '17e7362a63869199018e13f5974d38c2'  => [
                'title' => '花嫁丽舍私人婚礼会所',
                'address' => '浦东大道1868号东方城市大厦2楼（地铁6号线北洋泾路1号出口）'
            ],
            '4751a864a11983f8acd9ba3705f5b0d4'  => [
                'title' => '花嫁丽舍私人婚礼会所',
                'address' => '营口路699号 ，近国顺东路（地铁8号线黄兴公园站2号出口）'
            ],
            '243574e2fa39239f25ef5f655088b60a'  => [
                'title' => '花嫁丽舍海岛空中花园店',
                'address' => '丰台区南四环马家楼119号北门（南四环辅路右安路口,地铁4号线公益西桥站）'
            ],
            'caadc6e5489dd5175b7258d0abef1e83'  => [
                'title' => '花嫁丽舍高端私人婚礼会所',
                'address' => '江杨南路485号'
            ],
            '266bec59bc8404ddec3abd56d14aecec'  => [
                'title' => '花嫁丽舍婚礼艺术空间',
                'address' => '龙蟠路88号国展中心F区'
            ],
            'dbee9ade3c790afb791e47af134515e6'  => [
                'title' => '花嫁丽舍婚礼艺术空间',
                'address' => '马家楼隆晟华盾产业园区G2座'
            ]
        ];

        return $data;
    }
}
